package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.SensitiveMapper;
import com.heima.wemedia.service.SensitiveService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;

@Service
@Transactional
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, WmSensitive> implements SensitiveService {
    /**
     * 删除敏感词
     */
    @Override
    public ResponseResult sensitiveDelete(Integer id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);   //PARAM_REQUIRE(500,"缺少参数")
        }

        //2.查询敏感词，先判断有没有该id的敏感词，有才能删除
        WmSensitive wmSensitive = getById(id);
        if(wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);    //DATA_NOT_EXIST(1002,"数据不存在")
        }

        //3.删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     */
    @Override
    public ResponseResult sensitiveUpdate(WmSensitive wmSensitive) {
        //1、检查参数
        if(wmSensitive.getId() == null || null == wmSensitive){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);   //PARAM_REQUIRE(500,"缺少参数")
        }

        //2、修改
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询敏感词
     */
    @Override
    public ResponseResult sensitiveList(SensitiveDto dto){
        //1、检查参数
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);    //PARAM_INVALID(501,"无效参数")
        }

        //检查分页
        dto.checkParam();

        //2、条件查询
        IPage page = new Page<>(dto.getPage(), dto.getSize());
        //Page page = new Page(dto.getPage(), dto.getSize());   //第二种方法
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();

        //模糊查询
        if(StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(WmSensitive::getSensitives, dto.getName());
        }
        //时间倒序
        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        page = page(page, queryWrapper);

        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 新增敏感词
     */
    @Override
    public ResponseResult sensitiveSave(WmSensitive wmSensitive) {
        if(null == wmSensitive){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);  //PARAM_REQUIRE(500,"缺少参数")
        }

        WmSensitive sensitives = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));

        //已存在的敏感词，不能保存
        if(sensitives != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词已存在");
        }

        //2.保存
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }}
